<template>
  <!-- 容器点击时触发自定义 click 事件，传递 title 给父组件 -->
  <div class="container noselect" :class="{ 'downloading': isDownloading }" @click="!isDownloading && handleClick(id)">
    <div class="canvas">
      <div v-for="i in 25" :key="i" :class="['tracker', `tr-${i}`]" class="tracker"></div>
      <div class="notification">
        <div class="notiglow"></div>
        <div class="notiborderglow"></div>
        <div class="category">{{ category }}</div>
        <div class="notititle">{{ name }}</div>  <!-- 绑定原有数据 -->
        <div class="notibody">{{ isDownloading ? '正在下载...' : description }}</div>
        <!-- 下载状态指示器 -->
        <div v-if="isDownloading" class="download-indicator">
          <div class="spinner"></div>
        </div>
      </div>
    </div>
    <!-- 根据 showDelete 决定是否显示删除按钮 -->
    <button v-if="showDelete" @click.stop.prevent="handleDelete" class="button delete-button">
      <span class="X"></span>
      <span class="Y"></span>
      <div class="close">Delete</div>
    </button>
  </div>

</template>

<script setup lang="ts">
import { computed } from '@vue/reactivity';
import { checkUserSettings } from '../../electron/operation/usage/auth';
import { ElMessage, ElMessageBox } from 'element-plus';

// 定义组件属性接口
interface CardProps {
  id: string;
  name: string;
  category: string;
  description: string;
  subtitle?: string;
  showDelete?: boolean;
  isDownloading?: boolean;
}

// 定义组件属性
const props = defineProps<CardProps>();

// 定义事件发射器
const emit = defineEmits(['click', 'delete']);

// 计算属性：环境
const env = computed(() => {
  return props.category;
});

// 计算属性：中文名
const name = computed(() => {
  return props.name;
});

// 修改后的点击处理函数
const handleClick = async (id: string) => {
  try {
    const hasSettings = await checkUserSettings()
    if (!hasSettings) return
    
    emit('click', id)
  } catch (error) {
    console.error('权限检查失败:', error)
    ElMessage.error('系统配置检查异常')
  }
}

// 处理删除事件
const handleDelete = async () => {
  try {
    // 先删除本地文件
    // await deletePlugin(props.id);
    // 触发删除事件更新界面
    await ElMessageBox.confirm(
      `确定要删除此插件吗？`,
      '提示',
      {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning',
      }
    );
    emit('delete', props.id);
  } catch (error) {
    console.error('删除失败:', error);
    ElMessage.error('删除插件失败');
  }
};
</script>

<style scoped>
.container {
  position: relative;
  width: 200px;
  height: 200px;
  transition: 200ms;
  z-index: 1;
}

.notification {
  display: flex;
  flex-direction: column;
  isolation: isolate;
  position: relative;
  width: 15rem;
  height: 8rem;
  background: #29292c;
  border-radius: 1rem;
  overflow: hidden;
  font-family: 'Gill Sans', 'Gill Sans MT', Calibri, 'Trebuchet MS', sans-serif;
  font-size: 16px;
  --gradient: linear-gradient(to bottom, #2eadff, #3d83ff, #7e61ff);
  --color: #32a6ff;
  z-index: 1
}

.notification:before {
  position: absolute;
  content: "";
  inset: 0.0625rem;
  border-radius: 0.9375rem;
  background: #18181b;
  z-index: 2
}

.notification:after {
  position: absolute;
  content: "";
  width: 0.25rem;
  inset: 0.65rem auto 0.65rem 0.5rem;
  border-radius: 0.125rem;
  background: var(--gradient);
  transition: transform 300ms ease;
  z-index: 4;
}

.notification:hover:after {
  transform: translateX(0.15rem)
}

.category {
  color: #99999d;
  font-size: 1.1rem;
  transition: transform 300ms ease;
  z-index: 5;
  font-weight: bold;
  padding-top: 2%;

}

.notititle {
  color: var(--color);
  padding: 0rem 0rem 0rem 1.25rem;
  font-weight: 500;
  font-size: 1.1rem;
  transition: transform 300ms ease;
  z-index: 5;
  text-align: left;
}

.notification:hover .notititle {
  transform: translateX(0.15rem)
}

.notibody {
  color: #99999d;
  font-size: 0.9rem;
  padding: 0 1.25rem;
  transition: transform 300ms ease;
  z-index: 5;
  text-align: left;
}

.notification:hover .notibody {
  transform: translateX(0.25rem)
}

.notiglow,
.notiborderglow {
  position: absolute;
  width: 20rem;
  height: 20rem;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle closest-side at center, white, transparent);
  opacity: 0;
  transition: opacity 300ms ease;
}

.notiglow {
  z-index: 3;
}

.notiborderglow {
  z-index: 1;
}

.notification:hover .notiglow {
  opacity: 0.1
}

.notification:hover .notiborderglow {
  opacity: 0.1
}

.note {
  color: var(--color);
  position: fixed;
  top: 80%;
  left: 50%;
  transform: translateX(-50%);
  text-align: center;
  font-size: 0.9rem;
  width: 75%;
}

/* Delete */
.button {
  position: relative;
  width: 2em;
  height: 2em;
  border: none;
  background: rgba(180, 83, 107, 0.11);
  border-radius: 5px;
  transition: background 0.5s;
}

.X {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 1.5em;
  height: 1px;
  background-color: rgb(255, 255, 255);
  transform: translateX(-50%) rotate(45deg);
}

.Y {
  content: "";
  position: absolute;
  top: 50%;
  left: 50%;
  width: 1.5em;
  height: 1px;
  background-color: #fff;
  transform: translateX(-50%) rotate(-45deg);
}

.close {
  position: absolute;
  display: flex;
  padding: 0.6rem 1rem;
  align-items: center;
  justify-content: center;
  transform: translateX(-50%);
  top: 110%;
  left: 50%;
  width: 2.5em;
  height: 1.5em;
  font-size: 12px;
  background-color: rgb(19, 22, 24);
  color: rgb(187, 229, 236);
  border: none;
  border-radius: 3px;
  opacity: 0;
}

.button:hover {
  background-color: rgb(211, 21, 21);
}

.button:active {
  background-color: rgb(130, 0, 0);
}

.button:hover > .close {
  animation: close 0.2s forwards 0.25s;
}

@keyframes close {
  100% {
    opacity: 1;
  }
}

/* 下载状态样式 */
.container.downloading {
  opacity: 0.7;
  cursor: not-allowed;
  pointer-events: none;
}

.container.downloading .notification {
  background: #2a2a2d;
}

.download-indicator {
  position: absolute;
  top: 50%;
  right: 1rem;
  transform: translateY(-50%);
  z-index: 6;
}

.spinner {
  width: 20px;
  height: 20px;
  border: 2px solid #99999d;
  border-top: 2px solid var(--color);
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}

.delete-button {
  position: absolute;
  top: 5px;
  right: -30px;
  z-index: 2;
  width: 2em;
  height: 2em;
  border: none;
  background: rgba(180, 83, 107, 0.11);
  border-radius: 5px;
  transition: background 0.5s;
}
</style>
